<?php
include("../../header.inc");
require_once("../../DataBase.inc");

echo "<center><h1>Eligibility Rules</h1></center>";
echo "<br/>";

$programs = DataBase::query("SELECT * FROM selection_campaign_programs
            LEFT JOIN program ON program.program_id=selection_campaign_programs.program_id
            WHERE selection_campaign_programs.selection_campaign_id= ".$_SESSION["campaign_id"]);

foreach ($programs as $p) {
    if (isset($_POST[$p["program_id"]."_interview_communication_and_maturity_rule"])) {
        $q = "UPDATE interview_eligibility_rule SET ";
        $q .= "interview_communication_and_maturity_rule=".$_POST[$p["program_id"]."_interview_communication_and_maturity_rule"];
        $q .= ",interview_motivation_for_cist_rule=".$_POST[$p["program_id"]."_interview_motivation_for_cist_rule"];
        $q .= ",interview_affinities_with_computer_or_carear_plan_rule=".$_POST[$p["program_id"]."_interview_affinities_with_computer_or_carear_plan_rule"];
        $q .= " WHERE selection_campaign_id=".$_SESSION["campaign_id"]." AND program_id=".$p["program_id"];
        DataBase::query($q);
        
        $dataCheck = DataBase:: query("SELECT * FROM interview_eligibility_rule WHERE selection_campaign_id=".$_SESSION["campaign_id"]." AND program_id=".$p["program_id"]);

        if(DataBase::number_of_affected_rows()==0){
            DataBase::insert("interview_eligibility_rule",array(
                    "selection_campaign_id"=>$_SESSION["campaign_id"],
                    "program_id"=>$p["program_id"],
                    "interview_communication_and_maturity_rule"=>$_POST[$p["program_id"]."_interview_communication_and_maturity_rule"],
                    "interview_motivation_for_cist_rule"=>$_POST[$p["program_id"]."_interview_motivation_for_cist_rule"],
                    "interview_affinities_with_computer_or_carear_plan_rule"=>$_POST[$p["program_id"]."_interview_affinities_with_computer_or_carear_plan_rule"]
            ));
        }
           
      
        
//        if (DataBase::number_of_affected_rows()==0) {
//           echo Database:: number_of_affected_rows()."<br />";
//           if($p["program_id"]==null){
//            DataBase::insert("interview_eligibility_rule",array(
//                "selection_campaign_id"=>$_SESSION["campaign_id"],
//                "program_id"=>$p["program_id"],
//                "interview_communication_and_maturity_rule"=>$_POST[$p["program_id"]."_interview_communication_and_maturity_rule"],
//                "interview_motivation_for_cist_rule"=>$_POST[$p["program_id"]."_interview_motivation_for_cist_rule"],
//                "interview_affinities_with_computer_or_carear_plan_rule"=>$_POST[$p["program_id"]."_interview_affinities_with_computer_or_carear_plan_rule"]
//            ));
//
//            }
        //}
    }
}

echo"<form name='rules' method='post'>";
echo "<table border=1>";
echo"<th>Selection compang program</th>
    <th>Communication and Maturity rule</th>
    <th>Motivation for CIST rule</th>
    <th>Affinities with computer/Studie and Carear plan rule</th>";

foreach ($programs as $pro) {
    echo "<tr>";
    echo"<td>".$pro["program_name"]."</td>";

    echo"<td>";

    $interview_rules = DataBase::query("SELECT *
                FROM interview_eligibility_rule
                INNER JOIN program ON program.program_id = interview_eligibility_rule.program_id
                INNER JOIN selection_campaign_programs ON selection_campaign_programs.selection_campaign_id = interview_eligibility_rule.selection_campaign_id
                WHERE interview_eligibility_rule.program_id =".$pro["program_id"]."
                AND  interview_eligibility_rule.selection_campaign_id =".$_SESSION["campaign_id"]);

    echo "<select  name='".$pro["program_id"]."_interview_communication_and_maturity_rule'>";
    //foreach($interview_rules as $interview_rule){
    $interview_rule = $interview_rules[0];
    for ( $i = 0 ;$i < 6 ;$i++) {
        if ($i == $interview_rule['interview_communication_and_maturity_rule']) {
            echo"<option value='".$i."' selected='selected' >".$i."</option>";
        }else {
            echo"<option value='".$i."'>".$i."</option>";
        }
    }
    //}

    echo"</select>";
    echo"</td>";
    echo"<td>";
    echo "<select  name='".$pro["program_id"]."_interview_motivation_for_cist_rule'>";
    //foreach($interview_rules as $interview_rule) {
    for ( $j = 0 ;$j < 6 ;$j++) {
        if ($j == $interview_rule['interview_motivation_for_cist_rule']) {
            echo"<option value='".$j."' selected='selected' >".$j."</option>";
        }else {
            echo"<option value='".$j."'>".$j."</option>";
        }
    }
    //}
    echo"</select>";
    echo"</td>";

    echo"<td>";
    echo "<select  name='".$pro["program_id"]."_interview_affinities_with_computer_or_carear_plan_rule'>";
    // foreach($interview_rules as $interview_rule) {
    for ( $k = 0 ;$k < 6 ;$k++) {
        if ($k == $interview_rule['interview_affinities_with_computer_or_carear_plan_rule']) {
            echo"<option value='".$k."' selected='selected' >".$k."</option>";
        }else {
            echo"<option value='".$k."'>".$k."</option>";
        }
    }
    //}
    echo"</select>";
    echo"</td>";
    echo"</tr>";
}
echo "</table><input type='submit' value='Save' /></form>";

echo "<br/><a href='apply_rules.php'>Apply rules</a>";

include("../../footer.inc");
?>